<?php

/**
 * This is the model class for table "{{setting}}".
 *
 * The followings are the available columns in table '{{setting}}':
 * @property integer $zst_id
 * @property integer $zkl_type
 * @property string $zkl_title
 * @property string $zkl_variable
 * @property string $zkl_valued
 */
class Setting extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Setting the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{setting}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zkl_variable, zkl_valued', 'required'),			
			array('zkl_variable, zkl_title', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zst_id, zkl_type, zkl_title, zkl_variable, zkl_valued', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zst_id' => 'ID',
			'zkl_type' => '类型',
			'zkl_title' => '说明',
			'zkl_variable' => '配置名',
			'zkl_valued' => '配置值',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zst_id',$this->zst_id);
		$criteria->compare('zkl_type',$this->zkl_type);
		$criteria->compare('zkl_title',$this->zkl_title);
		$criteria->compare('zkl_variable',$this->zkl_variable,true);
		$criteria->compare('zkl_valued',$this->zkl_valued,true);

		$criteria->order = "zst_id DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//获取缓存
	public function getSettingCache(){
		$data = Yii::app()->filecache->get('setting');
		if(empty($data)){
			$data = Setting::model()->findAll();
			$data = CHtml::listData($data,'zkl_variable','zkl_valued');
			Yii::app()->filecache->set('setting',$data,3600*24*24*360);
		}
		return $data;
	}
	
	//返回站点类型
	public function getSiteType($type=null){
		if($type==null){
			return array(
					'1'=>'多店铺',
					'2'=>'单店铺',
					'3'=>'单店铺,多店铺共存',
			);
		}else{
			$level = $this->getSiteType();
			return $level[$type];
		}
	}
	
	//清空缓存
	public function destructCache(){
		Yii::app()->filecache->delete('setting');
		return true;
	}

	//保存后执行
	protected function AfterSave(){
		$this->destructCache();
		return true;
	}

	//删除后执行
	protected function AfterDelete(){
		$this->destructCache();
		return true;
	}

}